'use strict'

const Controller = require('egg').Controller

class SellerController extends Controller {
  async create() {
    const { ctx } = this
    let {
      brand,
      model,
      manufacturerYear,
      mileage,
      bodyType,
      seatsNo,
      doorsNo,
      vehicleTransmissionType,
      driveType,
      cylindersNo,
      fuelType,
      vimNumber,
      engineCapacity,
      gearsNo,
      price,
      truePrice,
      title,
      cover,
      medias,
      content,

      name,
      phoneNo,
      email
    } = ctx.request.body

    if (!cover) cover = medias[0]

    const create = {
      brand,
      model,
      manufacturerYear,
      mileage,
      bodyType,
      seatsNo,
      doorsNo,
      vehicleTransmissionType,
      driveType,
      cylindersNo,
      fuelType,
      vimNumber,
      engineCapacity,
      gearsNo,
      price,
      truePrice,
      title,
      cover,
      medias,
      content,

      name,
      phoneNo,
      email,
      active: false // 默认未审核
    }

    await ctx.model.Seller.create(create)
    ctx.body = create
  }

  async delete() {
    const { ctx } = this
    const { id } = ctx.params

    await ctx.model.Seller.destroy({
      where: {
        id
      }
    })
    ctx.body = { id }
  }

  async update() {
    const { ctx } = this
    const { id } = ctx.params

    let {
      brand,
      model,
      manufacturerYear,
      mileage,
      bodyType,
      seatsNo,
      doorsNo,
      vehicleTransmissionType,
      driveType,
      cylindersNo,
      fuelType,
      vimNumber,
      engineCapacity,
      gearsNo,
      price,
      truePrice,
      title,
      cover,
      medias,
      content,

      name,
      phoneNo,
      email,
      active
    } = ctx.request.body

    cover = medias[0]

    const update = {
      brand,
      model,
      manufacturerYear,
      mileage,
      bodyType,
      seatsNo,
      doorsNo,
      vehicleTransmissionType,
      driveType,
      cylindersNo,
      fuelType,
      vimNumber,
      engineCapacity,
      gearsNo,
      price,
      truePrice,
      title,
      cover,
      medias,
      content,

      name,
      phoneNo,
      email,
      active
    }

    await ctx.model.Seller.update(update, {
      where: { id }
    })
    ctx.body = update
  }

  async active() {
    const { ctx } = this
  }

  async list() {
    const { ctx } = this

    let { brand = '', model = '', price, page = 1, limit = 10 } = ctx.query

    const priceRange = [0, 10000000]
    const where = {
      brand: {
        $like: `%${brand}%`
      },
      model: {
        $like: `%${model}%`
      },
      price: {
        $between: price ? price.split(',') : priceRange
      }
    }

    const total = await ctx.model.Seller.count({
      where
    })

    limit = parseInt(limit)
    page = parseInt(page)

    const list = await ctx.model.Seller.findAll({
      attributes: [
        'id',
        'title',
        'brand',
        'model',
        'price',
        'name',
        'medias',
        'cover'
      ],
      limit,
      offset: limit * (page - 1),
      where
    })
    ctx.body = {
      total,
      list
    }
  }

  async id() {
    const { ctx } = this
    const { id } = ctx.params
    const data = await ctx.model.Seller.findOne({ where: { id } })

    ctx.body = data
  }
}

module.exports = SellerController
